<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Handling Nested Fields in Conditionals | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'ingest-conditional-nullcheck.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/ingest-conditional-nullcheck.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/ingest-conditional-nullcheck.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/ingest-conditional-nullcheck.html" rel="nofollow" target="_blank">../en/ingest-conditional-nullcheck.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="ingest.html">Ingest node</a></span>
»
<span class="breadcrumb-link"><a href="ingest-conditionals.html">Conditional Execution in Pipelines</a></span>
»
<span class="breadcrumb-node">Handling Nested Fields in Conditionals</span>
</div>
<div class="navheader">
<span class="prev">
<a href="ingest-conditionals.html">« Conditional Execution in Pipelines</a>
</span>
<span class="next">
<a href="ingest-conditional-complex.html">Complex Conditionals »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="ingest-conditional-nullcheck"></a>Handling Nested Fields in Conditionals<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/ingest/ingest-node.asciidoc">edit</a>
</h2>
</div></div></div>
<p>Source documents often contain nested fields. Care should be taken
to avoid NullPointerExceptions if the parent object does not exist
in the document. For example <code class="literal">ctx.a.b.c</code> can throw an NullPointerExceptions
if the source document does not have top level <code class="literal">a</code> object, or a second
level <code class="literal">b</code> object.</p>
<p>To help protect against NullPointerExceptions, null safe operations should be used.
Fortunately, Painless makes <a href="https://www.elastic.co/guide/en/elasticsearch/painless/7.7/painless-operators-reference.html#null-safe-operator" class="ulink" target="_top">null safe</a>
operations easy with the <code class="literal">?.</code> operator.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT _ingest/pipeline/drop_guests_network
{
  "processors": [
    {
      "drop": {
        "if": "ctx.network?.name == 'Guest'"
      }
    }
  ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1056.console"></div>
<p>The following document will get <a class="xref" href="drop-processor.html" title="Drop Processor">dropped</a> correctly:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST test/_doc/1?pipeline=drop_guests_network
{
  "network": {
    "name": "Guest"
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1057.console"></div>
<p>Thanks to the <code class="literal">?.</code> operator the following document will not throw an error.
If the pipeline used a <code class="literal">.</code> the following document would throw a NullPointerException
since the <code class="literal">network</code> object is not part of the source document.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST test/_doc/2?pipeline=drop_guests_network
{
  "foo" : "bar"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1058.console"></div>
<p>The source document can also use dot delimited fields to represent nested fields.</p>
<p>For example instead the source document defining the fields nested:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "network": {
    "name": "Guest"
  }
}</pre>
</div>
<p>The source document may have the nested fields flattened as such:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "network.name": "Guest"
}</pre>
</div>
<p>If this is the case, use the <a class="xref" href="dot-expand-processor.html" title="Dot Expander Processor">Dot Expand Processor</a>
so that the nested fields may be used in a conditional.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT _ingest/pipeline/drop_guests_network
{
  "processors": [
    {
      "dot_expander": {
        "field": "network.name"
      }
    },
    {
      "drop": {
        "if": "ctx.network?.name == 'Guest'"
      }
    }
  ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1059.console"></div>
<p>Now the following input document can be used with a conditional in the pipeline.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST test/_doc/3?pipeline=drop_guests_network
{
  "network.name": "Guest"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1060.console"></div>
<p>The <code class="literal">?.</code> operators works well for use in the <code class="literal">if</code> conditional
because the <a href="https://www.elastic.co/guide/en/elasticsearch/painless/7.7/painless-operators-reference.html#null-safe-operator" class="ulink" target="_top">null safe operator</a>
returns null if the object is null and <code class="literal">==</code> is null safe (as well as many other
<a href="https://www.elastic.co/guide/en/elasticsearch/painless/7.7/painless-operators.html" class="ulink" target="_top">painless operators</a>).</p>
<p>However, calling a method such as <code class="literal">.equalsIgnoreCase</code> is not null safe
and can result in a NullPointerException.</p>
<p>Some situations allow for the same functionality but done so in a null safe manner.
For example: <code class="literal">'Guest'.equalsIgnoreCase(ctx.network?.name)</code> is null safe because
<code class="literal">Guest</code> is always non null, but <code class="literal">ctx.network?.name.equalsIgnoreCase('Guest')</code> is not null safe
since <code class="literal">ctx.network?.name</code> can return null.</p>
<p>Some situations require an explicit null check. In the following example there
is not null safe alternative, so an explicit null check is needed.</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "drop": {
    "if": "ctx.network?.name != null &amp;&amp; ctx.network.name.contains('Guest')"
  }
}</pre>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="ingest-conditionals.html">« Conditional Execution in Pipelines</a>
</span>
<span class="next">
<a href="ingest-conditional-complex.html">Complex Conditionals »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>